<template>
  <div>
    <div class="n-layout-page-header">
      <n-card :bordered="false" title="菜单权限管理">页面数据为 Mock 示例数据，非真实数据.</n-card>
    </div>
    <n-grid class="mt-4" cols="1 s:1 m:1 l:3 xl:3 2xl:3" responsive="screen" :x-gap="12">
      <n-gi span="1">
        <n-card :bordered="false" size="small">
          <template #header>
            <n-space>
              <n-dropdown trigger="hover" :options="addMenuOptions" @select="selectAddMenu">
                <n-button type="info" ghost icon-placement="right">
                  添加菜单
                  <template #icon>
                    <div class="flex items-center">
                      <n-icon size="14">
                        <DownOutlined />
                      </n-icon>
                    </div>
                  </template>
                </n-button>
              </n-dropdown>
              <n-button type="info" ghost icon-placement="left" @click="packHandle">
                全部{{ expandedKeys.length ? '收起' : '展开' }}
                <template #icon>
                  <div class="flex items-center">
                    <n-icon size="14">
                      <AlignLeftOutlined />
                    </n-icon>
                  </div>
                </template>
              </n-button>
            </n-space>
          </template>
          <div class="w-full">
            <n-input v-model:value="pattern" placeholder="输入菜单名搜索">
              <template #suffix>
                <n-icon size="18" class="cursor-pointer">
                  <SearchOutlined />
                </n-icon>
              </template>
            </n-input>
            <div class="py-3">
              <n-tree
                block-line
                :data="treeData"
                :virtual-scroll="true"
                :pattern="pattern"
                :expandedKeys="expandedKeys"
                style="max-height: 650px; overflow: hidden"
                @update:selected-keys="selectedTree"
                @update:expanded-keys="onExpandedKeys"
              />
            </div>
          </div>
        </n-card>
      </n-gi>
      <n-gi span="2">
        <n-card :segmented="{ content: 'hard' }" :bordered="false" size="small">
          <template #header>
            <n-space>
              <n-icon size="18">
                <FormOutlined />
              </n-icon>
              <span>编辑菜单{{ treeItemTitle ? `：${treeItemTitle}` : '' }}</span>
            </n-space>
          </template>
          <n-alert type="info" closable>从菜单列表选择一项后，进行编辑</n-alert>
          <n-form
            :model="formParams"
            :rules="rules"
            ref="formRef"
            :label-width="100"
            label-placement="left"
            v-if="isEditMenu"
            class="py-4"
          >
            <n-form-item label="类型" path="type">
              <span>{{ formParams.type === 1 ? '侧边栏菜单' : '' }}</span>
            </n-form-item>
            <n-form-item label="标题" path="label">
              <n-input placeholder="请输入标题" v-model:value="formParams.label"></n-input>
            </n-form-item>
            <n-form-item label="副标题" path="subtitle">
              <n-input placeholder="请输入副标题" v-model:value="formParams.subtitle"></n-input>
            </n-form-item>
            <n-form-item label="路径" path="path">
              <n-input placeholder="请输入路径" v-model:value="formParams.path"></n-input>
            </n-form-item>
            <n-form-item label="打开方式" path="openType">
              <n-radio-group v-model:value="formParams.openType" name="openType">
                <n-space>
                  <n-radio :value="1">当前窗口</n-radio>
                  <n-radio :value="2">新窗口</n-radio>
                </n-space>
              </n-radio-group>
            </n-form-item>
            <n-form-item label="菜单权限" path="auth">
              <n-input placeholder="请输入权限，多个权限用，分割" v-model:value="formParams.auth"></n-input>
            </n-form-item>
            <n-form-item path="auth" style="margin-left: 100px">
              <n-space>
                <n-button type="primary" :loading="subLoading" @click="formSubmit">保存修改</n-button>
                <n-button @click="handleReset">重置</n-button>
              </n-space>
            </n-form-item>
          </n-form>
        </n-card>
      </n-gi>
    </n-grid>
    <createDrawer ref="createDrawerRef" :title="drawerTitle" />
  </div>
</template>
<script lang="ts" setup>
import { ref, computed, onMounted, reactive, unref } from 'vue';
import { useMessage } from 'naive-ui';
import { DownOutlined, AlignLeftOutlined, SearchOutlined, FormOutlined } from '@vicons/antd';
import { getMenuList } from '@/api/system/menu';
import { getTreeItem } from '@/utils';
import createDrawer from './createDrawer.vue';

let treeItemKey = ref([]);

const formRef: any = ref(null);
const isEditMenu = ref(false);
const pattern = ref('');
const treeData = ref([]);
const subLoading = ref(false);
const treeItemTitle = ref('');
const drawerTitle = ref('');
const createDrawerRef = ref();

const message = useMessage();

// 树节点展开
let expandedKeys = ref([]);

const isAddSon = computed(() => {
  return !treeItemKey.value.length;
});

const addMenuOptions = ref([
  {
    label: '添加顶级菜单',
    key: 'home',
    disabled: false
  },
  {
    label: '添加子菜单',
    key: 'son',
    disabled: isAddSon
  }
]);

const rules = {
  label: {
    required: true,
    message: '请输入标题',
    trigger: 'blur'
  },
  path: {
    required: true,
    message: '请输入路径',
    trigger: 'blur'
  }
};

const formParams = reactive({
  type: 1,
  label: '', // 标题
  subtitle: '', // 副标题
  path: '', // 路径
  auth: '',
  openType: 1 // 打开方式
});

const defaultValueRef = () => ({
  type: 1,
  label: '', // 标题
  subtitle: '', // 副标题
  path: '', // 路径
  auth: '',
  openType: 1 // 打开方式
});

// 添加菜单
const selectAddMenu = (key: string) => {
  drawerTitle.value = key === 'home' ? '添加顶栏菜单' : `添加子菜单：${treeItemTitle.value}`;
  openCreateDrawer();
};

function openCreateDrawer() {
  const { openDrawer } = createDrawerRef.value;
  openDrawer();
}

// 全部展开收起
const packHandle = () => {
  if (expandedKeys.value.length) {
    expandedKeys.value = [];
  } else {
    expandedKeys.value = unref(treeData).map((item: any) => item.key as string) as [];
  }
};

// 选中节点
const selectedTree = keys => {
  if (keys.length) {
    const treeItem = getTreeItem(unref(treeData), keys[0]);
    treeItemKey.value = keys;
    treeItemTitle.value = treeItem.label;
    Object.assign(formParams, treeItem);
    isEditMenu.value = true;
  } else {
    isEditMenu.value = false;
    treeItemKey.value = [];
    treeItemTitle.value = '';
  }
};

// 展开/收起
const onExpandedKeys = keys => {
  expandedKeys.value = keys;
};

// 重置
const handleReset = () => {
  // const treeItem =
  Object.assign(formParams, defaultValueRef());
};

// 保存
const formSubmit = () => {
  formRef.value.validate((errors: boolean) => {
    if (!errors) {
      message.error('抱歉，您没有该权限');
    } else {
      message.error('请填写完整信息');
    }
  });
};

onMounted(async () => {
  const treeMenuList = await getMenuList();
  treeData.value = treeMenuList;
  console.log('menus: ', treeMenuList);
});
</script>
